﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Sharp.DataTransferObject;
using Sharp.DataTransferObject.Workflow;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.ServiceContracts;

namespace Sharp.WebBooter.Controllers
{
    /// <summary>
    /// 流程定义XML操作控制器
    /// </summary>
    public class Wf2XmlController : ApiController
    {



        #region 读取流程XML文件数据处理

        /// <summary>
        /// 读取XML文件
        /// </summary>
        /// <returns></returns>
        [System.Web.Http.HttpGet]
        public Result QueryProcessFile(string processGuid, string version)
        {
            Result result = new Result();
            try
            {
                using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
                {
                    result.Data = proxy.Channel.GetProcessFile(processGuid, version);

                }
            }
            catch (System.Exception ex)
            {
                result.Err = string.Format("获取流程XML文件失败！{0}", ex.Message);
            }

            return result;
        }

        /// <summary>
        /// 查询流程文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [System.Web.Http.HttpGet]
        public Result QueryProcessFileByID(int id)
        {
            Result result = new Result();
            try
            {
                using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
                {
                    result.Data = proxy.Channel.GetProcessFileByID(id);
                }
            }
            catch (Exception ex)
            {
                result.Err = string.Format("获取流程XML文件失败！{0}", ex.Message);
            }

            return result;
        }

        /// <summary>
        /// 根据版本获取流程记录
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [System.Web.Http.HttpPost]
        public Result GetProcessByVersion([FromBody] ProcessDTO obj)
        {
            Result result = new Result();
            try
            {
                using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
                {
                    result.Data = proxy.Channel.GetProcessByVersion(obj.ProcessGUID, obj.Version);
                }
            }
            catch (System.Exception ex)
            {
                result.Err = string.Format("获取流程基本信息失败！{0}", ex.Message);
            }

            return result;
        }

        /// <summary>
        /// 获取流程记录列表
        /// </summary>
        /// <returns></returns>
        [System.Web.Http.HttpPost]
        public Result GetProcessListSimple()
        {
            Result result = new Result();
            try
            {
                using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
                {

                    result.Data = proxy.Channel.GetProcessListSimple().ToList();
                }
            }
            catch (System.Exception ex)
            {
                result.Err = string.Format("获取流程基本信息失败！{0}", ex.Message);
            }

            return result;
        }

        /// <summary>
        /// 获取当前使用版本的流程记录
        /// </summary>
        /// <param name="processGuid"></param>
        /// <returns></returns>
        [System.Web.Http.HttpPost]
        public Result GetProcess(string processGuid)
        {
            Result result = new Result();
            try
            {
                using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
                {
                    result.Data = proxy.Channel.GetProcess(processGuid);
                }
            }
            catch (System.Exception ex)
            {
                result.Err = string.Format("获取流程基本信息失败！{0}", ex.Message);
            }

            return result;
        }

        /// <summary>
        /// 保存XML文件
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [System.Web.Http.HttpPost]
        public Result SaveProcessFile([FromBody] ProcessFileDTO entity)
        {
            Result result = new Result();
            try
            {
                using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
                {

                    proxy.Channel.SaveProcessFile(entity);

                }
            }
            catch (System.Exception ex)
            {
                result.Err = string.Format("保存流程XML文件失败！{0}", ex.Message);
            }

            return result;
        }

        /// <summary>
        /// 获取待办状态的节点
        /// </summary>
        /// <param name="appInstanceID"></param>
        /// <param name="processGUID"></param>
        /// <returns>活动实例数据列表</returns>
        [System.Web.Http.HttpPost]
        public Result QueryReadyActivityInstance(string appInstanceID, string processGUID)
        {
            Result result = new Result();
            try
            {
                using (var proxy = new WebApiClientServiceProxy<IWorkflowService>())
                {

                    var itemList = proxy.Channel.GetRunningActivityInstance(appInstanceID, processGUID).ToList();

                    result.Data = itemList;
                }
            }
            catch (System.Exception ex)
            {
                result.Err = string.Format("获取待办任务数据失败, 异常信息:{0}", ex.Message);
            }

            return result;
        }

        ///// <summary>
        ///// 查询已完成转移数据
        ///// </summary>
        ///// <param name="query">查询实体</param>
        ///// <returns>转移实例列表</returns>
        //[HttpPost]
        //public ResponseResult<List<TransitionImage>> QueryCompletedTransitionInstance([FromBody] TransitionInstanceQuery query)
        //{
        //    var result = ResponseResult<List<TransitionImage>>.Default();
        //    try
        //    {
        //        var wfService = new WorkflowService();
        //        var itemList = proxy.Channel.GetTransitionInstanceList(query).ToList();

        //        result = ResponseResult<List<TransitionImage>>.Success(itemList);
        //    }
        //    catch (System.Exception ex)
        //    {
        //        result = ResponseResult<List<TransitionImage>>.Error(string.Format(
        //            "获取已完成转移数据失败, 异常信息:{0}",
        //            ex.Message));
        //    }
        //    return result;
        //}

        ///// <summary>
        ///// 获取完成状态的任务
        ///// </summary>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public ResponseResult<List<TaskViewEntity>> QueryCompletedTasks([FromBody] TaskQuery query)
        //{
        //    var result = ResponseResult<List<TaskViewEntity>>.Default();
        //    try
        //    {
        //        var taskList = new List<TaskViewEntity>();
        //        var wfService = new WorkflowService();
        //        var itemList = proxy.Channel.GetCompletedTasks(query);

        //        if (itemList != null)
        //        {
        //            taskList = itemList.ToList();
        //        }
        //        result = ResponseResult<List<TaskViewEntity>>.Success(taskList);
        //    }
        //    catch (System.Exception ex)
        //    {
        //        result = ResponseResult<List<TaskViewEntity>>.Error(string.Format(
        //            "获取已办任务数据失败, 异常信息:{0}",
        //            ex.Message));
        //    }
        //    return result;
        //}

        #endregion

        #region 角色资源数据获取

        /// <summary>
        /// 获取所有角色数据集
        /// </summary>
        /// <returns></returns>
        [System.Web.Http.HttpPost]
        public Result GetRoleAll()
        {
            var result = new Result();


            try
            {
                using (var proxy = new WebApiClientServiceProxy<IUserService>())
                {
                    var entity = proxy.Channel.GetRoles().ToList();

                    result.Data = entity;
                }
            }
            catch (System.Exception ex)
            {
                result.Err = string.Format("获取角色数据失败！{0}", ex.Message);
            }

            return result;
        }

        #endregion


    }
}